package com.weiyu.bultrasound.core.service.Impl;

import com.weiyu.bultrasound.common.util.MD5Util;
import com.weiyu.bultrasound.core.dao.ApplicationsMapper;
import com.weiyu.bultrasound.core.dao.UsersMapper;
import com.weiyu.bultrasound.core.model.common.StatusCode;
import com.weiyu.bultrasound.core.model.entity.Users;
import com.weiyu.bultrasound.core.model.request.LoginRequest;
import com.weiyu.bultrasound.core.service.LoginService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 *@description 
 *@author abner chow
 *@date 2019-08-13 15:37
 */

@Service
public class LoginServiceImpl implements LoginService {

    private static final Logger logger = LoggerFactory.getLogger(LoginServiceImpl.class);


    @Autowired
    ApplicationsMapper applicationsMapper;

    @Autowired
    UsersMapper usersMapper;

    /**
     * @description 移动端根据设备序列号登录
     * @Param: [handultsn]
     * @Return: java.lang.Boolean
     * @Author: abner chow
     * @Date: 2019/8/13 15:38
     */
    @Override
    public Map login4Mobile(String handultsn) {
        Map result = new HashMap();
        if (StringUtils.hasText(handultsn)) {
            int i = applicationsMapper.countByHandultSn(handultsn);
            if (i > 0) {
                logger.info("登录移动端成功，登录设备序列号：" + handultsn);
                result.put("flag", true);
            } else if (i == 0) {
                result.put("statusCode", StatusCode.HANDULTSN_ERROR.getCode());
                result.put("statusMessage", StatusCode.HANDULTSN_ERROR.getMeaning());
            }
        }
        return result;
    }


    /**
     * @description 后台根据用户账号或者手机号登陆
     * @Param: [param]
     * @Return: com.weiyu.bultrasound.core.model.request.LoginRequest
     * @Author: abner chow
     * @Date: 2019/8/15 14:10
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map login4Web(LoginRequest param) {
        Map resultMap = new HashMap();
        if (null != param && StringUtils.hasText(param.getUserName())) {
            Users users = usersMapper.getByNameOrMobile(param.getUserName());
            if (users != null) {
                String password = users.getPassword();
                String encryptedPwd = MD5Util.getMD5String(param.getPassword());
                if (password.equals(encryptedPwd)) {
                    users.setLoginDate(new Date());
                    usersMapper.updateById(users);
                    logger.info("登录管理后台成功，账号：" + param.getUserName());
                    resultMap.put("userInfo", users);
                } else {
                    resultMap.put("statusCode", "2001");
                    resultMap.put("statusMessage", "用户名或密码错误");
                }
            } else {
                resultMap.put("statusCode", "2002");
                resultMap.put("statusMessage", "未存在该用户");
            }
        } else {
            logger.error("客户端请求参数有误" + param.toString());
        }
        return resultMap;
    }
}
